GBML manual / Maths

abs

value without its sign - always zero or positive. abs(-7) and abs(7) are both 7.

Syntax

abs(value) -> real

Arguments

NameType
valuereal

Returns

real

Description

value without its sign - always zero or positive. abs(-7) and abs(7) are both 7.

The usual reason to reach for this: turning "how far apart are these two numbers" into a single comparison, without caring which one is bigger.

abs hands back a real, even for a whole number. If the number is counted - a slot number, a number of frames - and should stay a whole number, use abs_int instead.

Example

var off_target = abs(x - xstart);
if (off_target < 4) {
    x = xstart;
}

Snaps this object back onto its own spawn point once it has drifted within 4 pixels of it, whichever side it drifted to - without abs, this would need two separate comparisons (x > xstart - 4 && x < xstart + 4) to say the same thing.

Cost

A sign check - as cheap as this whole group of functions gets, safe to call as often as a frame needs.

On the GBA

A direct call to the GBA module’s own abs - a sign check, as cheap as this whole group gets. It hands back a real, even for a whole number: for counted things, abs_int keeps the answer a whole number.

See also: minmaxclampsignabs_int